package com.example.rbacmanage.service.app.impl;

import cn.hutool.core.util.StrUtil;
import com.example.rbacmanage.entity.app.Blog;
import com.example.rbacmanage.entity.app.Classic;
import com.example.rbacmanage.entity.app.Tag;
import com.example.rbacmanage.mapper.app.BlogMapper;
import com.example.rbacmanage.mapper.app.ClassicMapper;
import com.example.rbacmanage.mapper.app.TagMapper;
import com.example.rbacmanage.service.app.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private ClassicMapper classicMapper;
    @Autowired
    private TagMapper tagMapper;
    @Override
    public void save(Blog blog) {
        blogMapper.insert(blog);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            blogMapper.del(id);
        }
    }

    @Override
    public void update(Blog blog) {
        blogMapper.update(blog);
    }

    @Override
    public List<Blog> getAll(Blog blog) {
        List<Blog> list = blogMapper.list(blog);
        List<Long> classicIds = list.stream().map(Blog::getClassicId).distinct().toList();
        if (classicIds.size() > 0) {
            Classic classic = new Classic();
            classic.setIds(classicIds);
            List<Classic> classics = classicMapper.list(classic);
            for (Blog blog1 : list) {
                for (Classic classic1 : classics) {
                    if (classic1.getId().equals(blog1.getClassicId())){
                        blog1.setClassicName(classic1.getClassicName());
                        break;
                    }
                }
            }
        }
        for (Blog blog1 : list) {
            String tagIds = blog1.getTagId();
            if (StrUtil.isBlank(tagIds)){
                continue;
            }
            List<Long> tagIdList = Arrays.stream(tagIds.split(",")).map(Long::valueOf).toList();
            if (!CollectionUtils.isEmpty(tagIdList)){
                Tag tag = new Tag();
                tag.setIds(tagIdList);
                List<Tag> tags = tagMapper.list(tag);
                List<String> tagNames = tags.stream().map(Tag::getTagName).filter(Objects::nonNull).distinct().toList();
                blog1.setTagNames(tagNames);
            }
        }
        return list;
    }

    @Override
    public PageInfo<Blog> getPage(Blog blog) {
        PageHelper.startPage(blog.getPage(),blog.getLimit());
        return new PageInfo<>(getAll(blog));
    }

    @Override
    public Blog getById(Long id) {
        Blog blog = blogMapper.queryOneById(id);

        String tagIds = blog.getTagId();
        if (StrUtil.isNotBlank(tagIds)){
            List<Long> tagIdList = Arrays.stream(tagIds.split(",")).map(Long::valueOf).toList();
            if (!CollectionUtils.isEmpty(tagIdList)){
                Tag tag = new Tag();
                tag.setIds(tagIdList);
                List<Tag> tags = tagMapper.list(tag);
                List<String> tagNames = tags.stream().map(Tag::getTagName).filter(Objects::nonNull).distinct().toList();
                blog.setTagNames(tagNames);
            }
        }
        return blog;
    }

    @Override
    public Integer getCount(Blog blog) {
        return blogMapper.count(blog);
    }

    @Override
    public List<Blog> getListOrderByCreateTimeDesc(Blog blog) {
        return blogMapper.listOrderByCreateTimeDesc(blog);
    }
}
